﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Sample.Domain
{
    public class Person
    {
        protected Person()
        {
            PeerReviewerFor = new List<ReviewerForReview>();
            ReviewedIn = new List<PeerReview>();
        }

        [Required]
        [DataType(DataType.EmailAddress)]
        public virtual string Email { get; private set; }

        [Required]
        public virtual string FirstName { get; private set; }

        [Required]
        public virtual string LastName { get; private set; }

        [Required]
        public virtual Place Location { get; private set; }

        public virtual bool Active { get; set; }
        public virtual IList<ReviewerForReview> PeerReviewerFor { get; private set; }
        public virtual IList<PeerReview> ReviewedIn { get; private set; }

        public virtual long CountOfReviewsReceived { get; private set; }

        public virtual string FullName
        {
            get { return string.Format("{0} {1}", FirstName, LastName); }
        }

        public virtual void SetName(string first, string last)
        {
            if (last.IsNullOrEmpty()) throw new ArgumentNullException("last");
            if (first.IsNullOrEmpty()) throw new ArgumentNullException("first");
            FirstName = first;
            LastName = last;
        }

        public static Person Create(string email, string firstName, string lastName, Place location)
        {
            return new Person
                       {
                           Active = true,
                           Email = email,
                           FirstName = firstName,
                           LastName = lastName,
                           Location = location
                       };
        }

        public virtual void SetLocation(string description, double? latitude, double? longitude)
        {
            if (description.IsNullOrEmpty()) throw new ArgumentException("description");
            Location = Place.Create(description, latitude ?? 0, longitude ?? 0);
        }

        public override bool Equals(object obj)
        {
            return this.IsEqual(obj);
        }

        public override int GetHashCode()
        {
            return HashCodeGenerator.GenerateHashCode(Email);
        }
    }
}